Growl agent to add support for sending Growl notifications to network destinations

snicker 11 years ago
parent
commit
b944eb0e16
2 changed files with 62 additions and 0 deletions
  1. 1 0
      Gemfile
  2. 61 0
      app/models/agents/growl_agent.rb

+ 1 - 0
Gemfile

@@ -10,6 +10,7 @@ gem "rufus-scheduler", :require => false
10 10
 gem 'json', '>= 1.7.7'
11 11
 gem 'jsonpath'
12 12
 gem 'twilio-ruby'
13
+gem 'ruby-growl'
13 14
 
14 15
 gem 'delayed_job'
15 16
 gem 'delayed_job_active_record'#, "~> 0.3.3" # newer was giving a strange MySQL error

+ 61 - 0
app/models/agents/growl_agent.rb

@@ -0,0 +1,61 @@
1
+require 'ruby-growl'
2
+
3
+module Agents
4
+  class GrowlAgent < Agent
5
+
6
+    cannot_be_scheduled!
7
+    cannot_create_events!
8
+
9
+    description <<-MD
10
+      The GrowlAgent sends any events it receives to a Growl GNTP server immediately.
11
+      
12
+      It is assumed that events have a `message` or `text` key, which will hold the body of the growl notification, and a `subject` key, which will have the headline of the Growl notification. You can use Event Formatting Agent if your event does not provide these keys.
13
+
14
+      Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
15
+    MD
16
+
17
+    def default_options
18
+      {
19
+          'growlserver' => 'localhost',
20
+          'growlpassword' => '',
21
+          'growlappname' => 'HuginnGrowl',
22
+          'growlnotificationname' => 'Notification',
23
+          'expected_receive_period_in_days' => "2"
24
+      }
25
+    end
26
+    
27
+    def working?
28
+      last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
29
+    end
30
+
31
+    def validate_options
32
+      unless options['growlserver'].present? && options['expected_receive_period_in_days'].present?
33
+        errors.add(:base, "growlserver and expected_receive_period_in_days are required fields")
34
+      end
35
+    end
36
+    
37
+    def register_growl
38
+      @growler = Growl.new options['growlserver'], options['growlappname'], "GNTP"
39
+      @growler.password = options['growlpassword']
40
+      @growler.add_notification options['growlnotificationname']
41
+    end
42
+    
43
+    def notify_growl(subject, message)
44
+      @growler.notify(options['growlnotificationname'],subject,message)
45
+    end
46
+
47
+    def receive(incoming_events)
48
+      incoming_events.each do |event|
49
+        register_growl
50
+        message = (event.payload['message'] || event.payload['text']).to_s
51
+        subject = event.payload['subject'].to_s
52
+        if message != "" && subject != ""
53
+          log "Sending Growl notification '#{subject}': '#{message}' to #{options['growlserver']} with event #{event.id}"
54
+          notify_growl(subject,message)
55
+        else
56
+          log "Event #{event.id} not sent, message and subject expected"
57
+        end
58
+      end
59
+    end
60
+  end
61
+end